


/**************手机端rem尺寸算法  ****************/
var htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
var htmlDom = document.getElementsByTagName("html")[0];
htmlDom.style.fontSize = htmlWidth / 10 + "px";


/**************点击删除键删除商品*************/
var dels = document.getElementsByClassName("del");
for (let leg = dels.length, i = 0; i < leg; i++) {
    dels[i].onclick = function () {
        deleteItem(this);

    }
}

function deleteItem(th) {
    th.parentNode.parentNode.parentNode.remove();
}



/************全选商品************/
var allCheck = document.getElementsByClassName("all")[0];
allCheck.onclick = function () {
    let checkboxs = document.getElementsByClassName("checkbox");
    if (this.checked) {
        for (let i = 0, leg = checkboxs.length; i < leg; i++) {

            checkboxs[i].checked = true;

        }
    } else {
        for (let i = 0, leg = checkboxs.length; i < leg; i++) {

            checkboxs[i].checked = false;

        }
    }
}

// 单个点击复选框全按钮也会显示
var checkboxs = document.getElementsByClassName("checkbox");
for (let i = 0, leg = checkboxs.length; i < leg; i++) {
    checkboxs[i].onclick = function () {

        let num = 0;
        for (let j = 0, leg = checkboxs.length; j < leg; j++) {
            if (checkboxs[j].checked == true) {

                num++;
            }
        }


        if (num == checkboxs.length) {
            allCheck.checked = true;

        } else {
            allCheck.checked = false;
        }
    }

}






/********结算价格 ********/
//获取加减号
var red = this.document.getElementsByClassName("left");
var add = this.document.getElementsByClassName("right-1");
var inputs = this.document.getElementsByClassName("text");
var sum = document.getElementsByClassName("s-2-1")[0];


// 手动输入数字正确引导
for (let leg = inputs.length, i = 0; i < leg; i++) {
    inputs[i].onblur = function () {
        if (this.value >= 1 && (!(isNaN(this.value)))) {

            return;
        }
        this.value = "1";

        alert("请正确操作(输数字)");
    }
}

// 增商品时结算价格
for (i = 0; i < add.length; i++) {
    add[i].onclick = function () {
        let num = this.parentNode.getElementsByTagName("input")[0];
        num.value = Number(num.value) + 1;
        let price = this.parentNode.parentNode.getElementsByClassName("s-1")[0];
        sum.innerText = toDecimal2(price.innerText * num.value);


    }
}

// 减商品时结算价格
for (i = 0; i < red.length; i++) {
    red[i].onclick = function () {
        let num = this.parentNode.getElementsByTagName("input")[0];
        if (num.value > 1) {
            num.value = Number(num.value) - 1;
        }
        let price = this.parentNode.parentNode.getElementsByClassName("s-1")[0];

        sum.innerText = toDecimal2(price.innerText * num.value);

    }
}







/**
 * 保留2位小数，如：2，会在2后面补上00.即2.00
 * @param {*} x 
 */
function toDecimal2(x) {
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}








